<?php
/**
 * TopAdmin
 * 版权所有 2018-2027 TopAdmin，并保留所有权利。
 * Author: TopAdmin <8355763@qq.com>
 * Date: 2021/2/24
 */
namespace app\admin\controller\system;

use app\admin\model\AuthGroup;
use app\admin\model\AuthDept;
use app\admin\model\AuthRule;
use app\admin\model\Admin;
use app\admin\model\Adminlog;
use app\common\controller\Backend;
use app\common\traits\Curd;
use top\helper\SignHelper;
use top\helper\StringHelper;
use think\facade\Config;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Group extends Backend
{
    use Curd;
    public $uid = '';
    public function initialize()
    {
        $this->uid = Session::get('admin.id');
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->modelClass = new AuthGroup();
    }

    // 用户组添加
    public function add()
    {
        if (Request::isPost()) {
            $data = $this->request->post();
            try {
                $this->validate($data, 'AuthGroup');
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $data['status'] = 1;
            $result = AuthGroup::create($data);
            if ($result) {
                $this->success(lang('add success'), url('index'));
            } else {
                $this->error(lang('add fail'));
            }

        } else {
            $view = [
                'info' => null
            ];
            View::assign($view);
            return view('edit');
        }
    }

    // 用户组修改
    public function edit()
    {
        if (Request::isPost()) {
            $data = $this->request->post();
            if($data['id']==1){
                $this->error(lang('supper man cannot edit'));
            }
            try{
                $this->validate($data, 'AuthGroup');
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $where['id'] = $data['id'];
            $res = AuthGroup::update($data, $where);
            if($res){

                $this->success(lang('edit success'), url('index'));
            }else{
                $this->error(lang('edit fail'));

            }

        } else {
            $id = Request::param('id');
            $info = AuthGroup::find(['id' => $id]);
            $view = [
                'info' => $info,
                'title' => lang('edit')
            ];
            View::assign($view);
            return view();
        }
    }


    // 用户组显示权限
    public function access()
    {
        $admin_rule = AuthRule::field('id, pid, title')
            ->where('status',1)
            ->order('sort asc')->cache(3600)
            ->select()->toArray();
        $rules = AuthGroup::where('id', Request::param('id'))
            ->where('status',1)
            ->value('rules');
        $list = AuthRule::authChecked($admin_rule, $pid = 0, $rules);
        $group_id = Request::param('id');
        $idList = $this->modelClass->column('id');
        sort($idList);
        $view = [
            'list' => $list,
            'idList' => $idList,
            'group_id' => $group_id,
        ];
        View::assign($view);
        return view('access');
    }

    // 用户组保存权限
    public function setaccess()
    {
        $rules = $this->request->post('rules');
        if (empty($rules)) {
            $this->error(lang('please choose rule'));
        }
        $data = $this->request->post();
        $rules = AuthRule::authNormal($rules);
        $rls = '';
        foreach ($rules as $k=>$v){
            $rls.=$v['id'].',';
        }
        $where['id'] = $data['group_id'];
        $where['rules'] = $rls;
        if (AuthGroup::update($where)) {
            $admin = Session::get('admin');
            $admin['rules'] = $rls;
            Session::set('admin', $admin);
            $this->success(lang('rule assign success'),url('index'));
        } else {
            $this->error(lang('rule assign fail'));
        }
    }


}